# GF_PY3_CLASS_Finance_Entanglement_Theory_s_SQLITE3.py
# Create By GF 2024-01-22 18:16

import sqlite3  # Import Validation: SQLite3 2.6.0 from Python 3.12.0 / Requires SQLite 3.7.15 or Newer
# ..................................................
import GF_PY3_CLASS_Finance_Entanglement_Theory

# ##################################################

class Finance_Entanglement_Theory_s_SQLITE3(GF_PY3_CLASS_Finance_Entanglement_Theory.Finance_Entanglement_Theory):

    def __init__(self, SQLITE3_CONNECT, DB_TABLE:str):

        # 注意:
        # 需要在函数外创建 SQLite 3 数据库连接 (以便于复用及提高代码效率)
        # 示例:
        # >>> SQLITE3_CONNECT = sqlite3.connect("./EXAMPLE.db")  # 连接到 SQLite 3 数据库 (如果文件不存在, 会自动创建)
        # >>> SQLITE3_CONNECT.commit()                           # SQLIT3 提交事务 (查询无需 Commit, 删改需要 Commit 使事务生效)
        # >>> SQLITE3_CONNECT.close()                            # SQLIT3 关闭 Connection

        self.SQLITE3_CONNECT = SQLITE3_CONNECT
        self.DB_TABLE:str = DB_TABLE
        # ..........................................
        self.TRS_Left_Exists:int = 0            # 子类 (派生类) 会覆盖父类 (基类) 的 __init__ 方法。
        self.TRS_Top_Exists:int = 0             # 子类 (派生类) 会覆盖父类 (基类) 的 __init__ 方法。
        # ..........................................
        self.BRS_Left_Exists:int = 0            # 子类 (派生类) 会覆盖父类 (基类) 的 __init__ 方法。
        self.BRS_Bottom_Exists:int = 0          # 子类 (派生类) 会覆盖父类 (基类) 的 __init__ 方法。
        # ..........................................
        self.TRS_Left_UpperEdge:float = None    # 子类 (派生类) 会覆盖父类 (基类) 的 __init__ 方法。
        self.TRS_Left_LowerEdge:float = None    # 子类 (派生类) 会覆盖父类 (基类) 的 __init__ 方法。
        # ..........................................
        self.BRS_Left_UpperEdge:float = None    # 子类 (派生类) 会覆盖父类 (基类) 的 __init__ 方法。
        self.BRS_Left_LowerEdge:float = None    # 子类 (派生类) 会覆盖父类 (基类) 的 __init__ 方法。
        # ..........................................
        self.TRS_Top_UpperEdge:float = None     # 子类 (派生类) 会覆盖父类 (基类) 的 __init__ 方法。
        self.TRS_Top_LowerEdge:float = None     # 子类 (派生类) 会覆盖父类 (基类) 的 __init__ 方法。
        # ..........................................
        self.BRS_Bottom_UpperEdge:float = None  # 子类 (派生类) 会覆盖父类 (基类) 的 __init__ 方法。
        self.BRS_Bottom_LowerEdge:float = None  # 子类 (派生类) 会覆盖父类 (基类) 的 __init__ 方法。
        # ..........................................
        self.TRS_Group_Top = 1
        self.BRS_Group_Top = 1

    def UPDATE_OR_INSERT_ETG_Top_Reversal_Shape(self, ID:int, ROW_NUM:int, Input_UpperEdge:float, Input_LowerEdge:float) -> dict:

        Affected_Numbers = 0
        # ..........................................
        Cursor = self.SQLITE3_CONNECT.cursor()  # 创建一个 Cursor 对象并调用其 execute() 方法执行 SQL 命令
        # ..........................................
        ETG_TRS_Value = self.Top_Reversal_Shape(ROW_NUM = ROW_NUM, Input_UpperEdge = Input_UpperEdge, Input_LowerEdge = Input_LowerEdge)
        # ..........................................
        # Possible Errors: sqlite3.OperationalError: no such column: None
        # SQLite 3 does not Support None, NULL should be Used Instead.
        ETG_TRS_Value = "NULL" if (ETG_TRS_Value == None) else ETG_TRS_Value
        # ..........................................
        Cursor.execute(f"SELECT COUNT(*) AS ID_COUNT FROM {self.DB_TABLE} WHERE id = {ID}")
        Queried = Cursor.fetchone()  # 获取查询到的第 1 个结果
        ID_COUNT = Queried[0]        # 获取查询结果第 1 个值
        # ..........................................
        if (ID_COUNT == 0):
            Cursor.execute(f"INSERT INTO {self.DB_TABLE} (id, etg_trs) VALUES ({ID}, {ETG_TRS_Value});")
            Affected_Numbers = Cursor.rowcount  # 获取受影响的行数
        if (ID_COUNT >= 1):
            Cursor.execute(f"UPDATE {self.DB_TABLE} SET etg_trs = {ETG_TRS_Value} WHERE id = {ID};")
            Affected_Numbers = Cursor.rowcount  # 获取受影响的行数
        # ..........................................
        Cursor.close()  # SQLIT3 关闭 Cursor
        # ..........................................
        return {"affected_numbers": Affected_Numbers, "value": ETG_TRS_Value}

    def UPDATE_OR_INSERT_ETG_Bottom_Reversal_Shape(self, ID:int, ROW_NUM:int, Input_UpperEdge:float, Input_LowerEdge:float) -> dict:

        Affected_Numbers = 0
        # ..........................................
        Cursor = self.SQLITE3_CONNECT.cursor()
        # ..........................................
        ETG_BRS_Value = self.Bottom_Reversal_Shape(ROW_NUM = ROW_NUM, Input_UpperEdge = Input_UpperEdge, Input_LowerEdge = Input_LowerEdge)
        # ..........................................
        # Possible Errors: sqlite3.OperationalError: no such column: None
        # SQLite 3 does not Support None, NULL should be Used Instead.
        ETG_BRS_Value = "NULL" if (ETG_BRS_Value == None) else ETG_BRS_Value
        # ..........................................
        Cursor.execute(f"SELECT COUNT(*) AS ID_COUNT FROM {self.DB_TABLE} WHERE id = {ID}")
        Queried = Cursor.fetchone()
        ID_COUNT = Queried[0]
        # ..........................................
        if (ID_COUNT == 0):
            Cursor.execute(f"INSERT INTO {self.DB_TABLE} (id, etg_brs) VALUES ({ID}, {ETG_BRS_Value});")
            Affected_Numbers = Cursor.rowcount
        if (ID_COUNT >= 1):
            Cursor.execute(f"UPDATE {self.DB_TABLE} SET etg_brs = {ETG_BRS_Value} WHERE id = {ID};")
            Affected_Numbers = Cursor.rowcount
        # ..........................................
        Cursor.close()
        # ..........................................
        return {"affected_numbers": Affected_Numbers, "value": ETG_BRS_Value}

    def UPDATE_OR_INSERT_ETG_Top_Reversal_Shape_s_Group_Top(self, ID:int, ROW_NUM:int, Input_UpperEdge:float, Input_LowerEdge:float) -> dict:

        Affected_Numbers = 0
        # ..........................................
        Cursor = self.SQLITE3_CONNECT.cursor()
        # ..........................................
        ETG_T_Group_Value = self.Top_Reversal_Shape_s_Group_Top(ROW_NUM = ROW_NUM, Input_UpperEdge = Input_UpperEdge, Input_LowerEdge = Input_LowerEdge)
        # ..........................................
        # Possible Errors: sqlite3.OperationalError: no such column: None
        # SQLite 3 does not Support None, NULL should be Used Instead.
        ETG_T_Group_Value = "NULL" if (ETG_T_Group_Value == None) else ETG_T_Group_Value
        # ..........................................
        Cursor.execute(f"SELECT COUNT(*) AS ID_COUNT FROM {self.DB_TABLE} WHERE id = {ID}")
        Queried = Cursor.fetchone()
        ID_COUNT = Queried[0]
        # ..........................................
        if (ID_COUNT == 0):
            Cursor.execute(f"INSERT INTO {self.DB_TABLE} (id, etg_t_group) VALUES ({ID}, {ETG_T_Group_Value});")
            Affected_Numbers = Cursor.rowcount
        if (ID_COUNT >= 1):
            Cursor.execute(f"UPDATE {self.DB_TABLE} SET etg_t_group = {ETG_T_Group_Value} WHERE id = {ID};")
            Affected_Numbers = Cursor.rowcount
        # ..........................................
        Cursor.close()
        # ..........................................
        return {"affected_numbers": Affected_Numbers, "value": ETG_T_Group_Value}

    def UPDATE_OR_INSERT_ETG_Bottom_Reversal_Shape_s_Group_Bottom(self, ID:int, ROW_NUM:int, Input_UpperEdge:float, Input_LowerEdge:float) -> dict:

        Affected_Numbers = 0
        # ..........................................
        Cursor = self.SQLITE3_CONNECT.cursor()
        # ..........................................
        ETG_B_Group_Value = self.Bottom_Reversal_Shape_s_Group_Bottom(ROW_NUM = ROW_NUM, Input_UpperEdge = Input_UpperEdge, Input_LowerEdge = Input_LowerEdge)
        # ..........................................
        # Possible Errors: sqlite3.OperationalError: no such column: None
        # SQLite 3 does not Support None, NULL should be Used Instead.
        ETG_B_Group_Value = "NULL" if (ETG_B_Group_Value == None) else ETG_B_Group_Value
        # ..........................................
        Cursor.execute(f"SELECT COUNT(*) AS ID_COUNT FROM {self.DB_TABLE} WHERE id = {ID}")
        Queried = Cursor.fetchone()
        ID_COUNT = Queried[0]
        # ..........................................
        if (ID_COUNT == 0):
            Cursor.execute(f"INSERT INTO {self.DB_TABLE} (id, etg_b_group) VALUES ({ID}, {ETG_B_Group_Value});")
            Affected_Numbers = Cursor.rowcount
        if (ID_COUNT >= 1):
            Cursor.execute(f"UPDATE {self.DB_TABLE} SET etg_b_group = {ETG_B_Group_Value} WHERE id = {ID};")
            Affected_Numbers = Cursor.rowcount
        # ..........................................
        Cursor.close()
        # ..........................................
        return {"affected_numbers": Affected_Numbers, "value": ETG_B_Group_Value}

# EOF Signed by GF.
